package makabakaquestionnaire.zutsoftware.framework.security.handle;

import com.alibaba.fastjson.JSON;
import makabakaquestionnaire.zutsoftware.constant.HttpStatus;
import makabakaquestionnaire.zutsoftware.constant.Result;
import makabakaquestionnaire.zutsoftware.utils.ServletUtils;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author ：wjf
 * @date ：Created in 2022/2/20 12:42
 * @description：权限不足处理类
 * @modified By：
 */
@Component
public class AccessDeniedHandlerImpl implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        int code= HttpStatus.UNAUTHORIZED;
        ServletUtils.renderString(response, JSON.toJSONString(Result.restResult("认证失败",code,"权限不足")));
    }
}
